package com.example.administrator.demo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Button btnDB;
    private Button btnTable;
    private Button btnQuery;
    private Button btnInsert;
    private Button btnDel;
    private Button btnRead;
    private EditText editNum;
    private EditText editName;
    private EditText editAge;
    private TextView txtData;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();

        btnDBOnClick();

        btnTableOnClick();

        btnInsertOnClick();

        btnReadOnClick();

        btnDelOnClick();
    }

    private void btnDelOnClick() {
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除数据
                delStu();
            }
        });
    }

    private void delStu() {
        //删除：第一种
        String sql = "delete from student where number = 1001";
        db.execSQL(sql);

        //删除：第二种
        //删除条件
        String whereClause = "number=?";
        //删除条件参数
        String[] whereArgs = {String.valueOf(1004)};
        db.delete("student",whereClause,whereArgs);
    }

    private void btnReadOnClick() {
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从数据库中读取数据
                readDataFromDB();
            }
        });
    }

    private void readDataFromDB() {
        if(db == null){
            createOrOpenDB();
        }

        Cursor cursor = db.query("student",null,null,null,
                null,null,null);

        StringBuffer buffer = new StringBuffer();
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
            String number = cursor.getString(0);
            String name = cursor.getString(1);
            int age = cursor.getInt(2);
            buffer.append(number).append("  ").append(name).append("  ").append(age).append("\n");
        }
        txtData.setText(buffer);
    }

    private void btnInsertOnClick() {
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //插入数据
                insertStu();
                //自动读取
                readDataFromDB();
            }
        });
    }

    private void insertStu() {
        if(db == null){
            createOrOpenDB();
        }

        //获取数据
        String number = editNum.getText().toString();
        String name = editName.getText().toString();
        int age = Integer.parseInt(editAge.getText().toString());

        //插入：第一种
        String sql = "insert into student values('"+ number +"','"+ name +"',"+ age +")";
        db.execSQL(sql);

        //插入：第二种
       /* ContentValues values = new ContentValues();
        values.put("number",number);
        values.put("name",name);
        values.put("age",age);
        db.insert("student",null,values);*/
    }

    private void btnTableOnClick() {
        btnTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建学生表
                createStuTable();
            }
        });
    }

    private void createStuTable() {
        if(db == null){
            createOrOpenDB();
        }

        String sql = "create table if not exists student(number varchar(20), name varchar(20), age int)";
        db.execSQL(sql);
    }

    private void btnDBOnClick() {
        btnDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建/打开数据库
                createOrOpenDB();
            }
        });
    }

    private void createOrOpenDB() {
        String path = getFilesDir().getAbsolutePath() + File.separator + "stu.db";
        Log.e("MainActivity", "path = "+ path);
        db = SQLiteDatabase.openOrCreateDatabase(path,null);
    }

    private void findViews() {
        btnDB = findViewById(R.id.btn_DB);
        btnTable = findViewById(R.id.btn_table);
        btnQuery = findViewById(R.id.btn_query);
        btnInsert = findViewById(R.id.btn_insert);
        btnRead = findViewById(R.id.btn_read);
        btnDel = findViewById(R.id.btn_del);
        editNum = findViewById(R.id.editNum);
        editName = findViewById(R.id.editName);
        editAge = findViewById(R.id.editAge);
        txtData = findViewById(R.id.txt_data);
    }
}
